import { UserProfilesAPI } from '@/request/api';
import { defineStore } from 'pinia'
import { userKey } from "./keys";

interface IUserState {
    userInfo: IUserInfo,
    isLogined: Boolean,
    cartTotal: Number
}
interface IUserInfo {
    headImg: string,
    nickName: string,
    coin: number
}

export const useStore = defineStore(userKey, {
    state(): IUserState {
        return {
            userInfo: {
                // 头像
                headImg: new URL('../../assets/img/service.848ec511.png', import.meta.url).href,
                // 昵称
                nickName: '昵称',
                // 积分
                coin: 0
            },
            cartTotal: 0,
            isLogined: false
        }
    },
    actions: {
        initUserInfo() {
            this.userInfo = {
                // 头像
                headImg: new URL('../../assets/img/service.848ec511.png', import.meta.url).href,
                // 昵称
                nickName: '昵称',
                // 积分
                coin: 0
            };
            this.cartTotal = 0;
            this.isLogined = false
        },
        async registerUser() {
            try {
                let res = await UserProfilesAPI()
                this.userInfo = res.data.userInfo
                this.cartTotal = res.data.cartTotal
            } catch (error) {
                return error
            }
        },
    },
})